//
//  ApplyDecryptViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/5/8.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "ApplyDecryptViewController.h"
#import "UITextView+Placeholder.h"
#import "IQKeyboardManager.h"

@interface ApplyDecryptViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UIView *backV;

@end

@implementation ApplyDecryptViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     [IQKeyboardManager sharedManager].shouldShowToolbarPlaceholder = NO;
    self.navigationItem.title = @"预约私聊解密";
    self.topTextView.placeholder = @"详细的问题描述有助于同事有的放矢的，更加有针对性的解决你的实际问题。";
    self.bottomTextView.placeholder = @"你填写的信息只有同事可以看到，不会公开给其他人。";
    self.titleL.text = self.titleStr;
    self.priceL.text = [NSString stringWithFormat:@"￥%@", self.price];
    
    [self setupPopupView];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].shouldShowToolbarPlaceholder = YES;
}
- (void)setupPopupView {
    UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    backV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    self.backV = backV;
    
    UIView *whiteV = [[UIView alloc] initWithFrame:CGRectMake(38, 251, kScreenWidth - 76, 194)];
    whiteV.backgroundColor = [UIColor whiteColor];
    whiteV.layer.cornerRadius = 10.0f;
    whiteV.layer.masksToBounds = YES;
    [backV addSubview:whiteV];
    
    UIImageView *imageIV = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 87) * 0.5, 202, 87, 84)];
    imageIV.image = [UIImage imageNamed:@"decrypt_submit_success"];
//    imageIV.backgroundColor = UIColorFromRGB(0xFAB600);
    imageIV.backgroundColor = [UIColor clearColor];
    [backV addSubview:imageIV];
    
    UILabel *oneL = [[UILabel alloc] initWithFrame:CGRectMake(0, 65, whiteV.mj_w, 16)];
    oneL.textColor = UIColorFromRGB(0x383F42);
    oneL.textAlignment = NSTextAlignmentCenter;
    oneL.text = @"提交成功";
    oneL.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
    [whiteV addSubview:oneL];
    
    UILabel *twoL = [[UILabel alloc] initWithFrame:CGRectMake(0, 111, whiteV.mj_w, 13)];
    twoL.textColor = UIColorFromRGB(0x9DB1BA);
    twoL.textAlignment = NSTextAlignmentCenter;
    twoL.numberOfLines = 0;
    twoL.text = @"认证同事将在24小时内反馈是否预约成功，请耐心等待";
    twoL.font = [UIFont fontWithName:@"PingFangSC-Light" size:13];
    [whiteV addSubview:twoL];
    
    UIButton *closeB = [[UIButton alloc] initWithFrame:CGRectMake(0, whiteV.mj_h - 41, whiteV.mj_w, 41)];
    [closeB setBackgroundColor:UIColorFromRGB(0x00BCCC)];
    [closeB setTitle:@"我知道了" forState:UIControlStateNormal];
    closeB.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Light" size:14];
    [closeB setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [closeB addTarget:self action:@selector(backViewOnPress) forControlEvents:UIControlEventTouchUpInside];
    [whiteV addSubview:closeB];
}

- (void)backViewOnPress {
    [self.backV removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString *result = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (result.length > 300) {
        [CommonUtils showToast:@"最多输入300个字符"];
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
//    NSLog(@"textViewDidChange---%@", textView.text);
    if (textView == self.topTextView) {
        self.topCountL.text = [NSString stringWithFormat:@"%zd/300", textView.text.length];
    } else {
        self.bottomCountL.text = [NSString stringWithFormat:@"%zd/300", textView.text.length];
    }
}

#pragma mark - 付款按钮点击事件

- (IBAction)payBtnClick:(id)sender {
    if (!self.topTextView.text || self.topTextView.text.length == 0) {
        [CommonUtils showToast:@"请告诉同事你最想请教的问题"];
        return;
    }
    if (!self.bottomTextView.text || self.bottomTextView.text.length == 0) {
        [CommonUtils showToast:@"请描述一下你的个人情况"];
        return;
    }
    WS(weakSelf)
     [self showProgressWithView:self.view animated:YES];
    [SendRequest orderDecryptWithIncumbentDecryptId:self.incumbentDecryptId andQuestion:self.topTextView.text andPersonalSituation:self.bottomTextView.text result:^(NSDictionary *result, NSError *error) {
        
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            [ [CommonUtils sharedCommonUtils] payWithType:@"GOODS_TYPE_JM" relatedId:[NSString stringWithFormat:@"%@",result[@"data"][@"decryptId"]] result:^(NSDictionary *result, BOOL paySuccess) {
                [weakSelf hideProgress:self.view animated:YES];
                
                if (paySuccess) {
                    [[UIApplication sharedApplication].keyWindow addSubview:weakSelf.backV];
                }else{
//                    [CommonUtils showToast:@"支付失败，请在我的订单中完成支付"];
                }
            }];
            
        } else {
            [weakSelf hideProgress:self.view animated:YES];
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

@end
